/**
 *
 * @(#)MainContentPresenter.java 2011
 *
 *  Copyright 2011 classic-commerce
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 * @author: <a href="mailto:richard@ioniq.ca">Richard Mutezintare</a>
 *
 */

package ca.ioniq.client.presenter;

import java.util.ArrayList;
import java.util.Map;

import ca.ioniq.client.event.BrandLogoClickedEvent;
import ca.ioniq.client.view.MainContentView;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.DoubleClickEvent;
import com.google.gwt.event.dom.client.DoubleClickHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.ListBox;


public class MainContentPresenter implements Presenter {
    private MainContentView mainContentView;
    private EventBus eventBus;

    public MainContentPresenter(final MainContentView view, final EventBus eventBus) {
        mainContentView = view;
        this.eventBus = eventBus;

        registerEvents();
    }

    private void registerEvents() {

        // register country selection
        final ListBox countryList = mainContentView.getRegisterWidget().getCountryListBox();
        countryList.addChangeHandler(new ChangeHandler() {
            public void onChange(ChangeEvent event) {
                // get selected country
                String selectedValue = countryList.getItemText(countryList.getSelectedIndex());

//                supposedly we already have this from the remote service
                // Map<String, List<String>> countryToProvinceMap = new HashMap<String, List<String>>();
                // List<String> provinceList = countryToProvinceMap.get(selectedValue);
                // mainContentView.getRegisterWidget().setProvinceList(provinceList);

                // TODO implement me
                mainContentView.getRegisterWidget().setProvinceList(new ArrayList<String>());
            }
        });


        // register event on brand logo
        final Map<Image, Integer> brandLogoMap = mainContentView.getBrandSelectorWidget().getBrandMap();
        for(final Image brandLogo : brandLogoMap.keySet()) {
            brandLogo.addDoubleClickHandler(new DoubleClickHandler() {
                public void onDoubleClick(DoubleClickEvent event) {
                    int brandId = brandLogoMap.get(brandLogo);
                    eventBus.fireEvent(new BrandLogoClickedEvent(brandId));
                }
            });
        }
    }



    public void go(HasWidgets container) {

    }
}
